<?php

class EmptyAction extends CommonAction
{
    public $categoryModel;
    public $newsModel;
    public $pagenav;
    public $template;

    public function _initialize()
    {
        parent::_initialize();
        $this->categoryModel = D('Category');
        $this->newsModel = D('News');
        $ModulesDB = D('Modules');
        $cate = $this->categoryModel->where(array('name' => array('like', strtolower(MODULE_NAME) . "%"), 'site_id' => __RELATEID__))->find();
        //echo $this->categoryModel->getLastSql();exit;
        if (!is_array($cate)) {
            $this->_empty();
        }
        if (!array($cate)) {

            $this->_empty();
        } else {
            if ($cate['permission'] >= 0) {
                if (!session('memberInfo')) {
                    $this->error("此内容必须登录才能查看", U('Member/Public/login'));
                } else {
                    if (intval($this->memberInfo['socre']) < intval($cate['permission'])) {
                        $this->error("您积分不够,无法查看此内容");
                    }
                }
            }
        }
        if (!empty($this->curcate['out_link'])) {
            header("location:" . $this->curcate['out_link']);
        }
        //页面导航数组
        $this->pagenav = $this->getPageNav($cate['id']);
        $this->template = $this->getCurModelName();
        $this->assign('pagenav', $this->pagenav);

        $CategoryList = $this->categoryModel->where(array('site_id' => __RELATEID__, 'pid' => $this->pagenav[0]['id']))->order('sort')->select();
        $this->assign('CategoryList', $CategoryList);
        //栏目轮播图

        foreach ($this->pagenav as $v) {
            if (count(unserialize($v['banner'])) > 0) {
                $this->assign('banner', unserialize($v['banner']));
            }
        }


        //dump(unserialize($cate['banner']));
        $this->curcate = $cate;
        //操作名是否为数字,由此判断页面是否为列表
        if (intval(ACTION_NAME) == 0) {
            //产生分页变量
            //dump(C('VAR_PAGE'));
            //dump(strpos(ACTION_NAME,C('VAR_PAGE')));
            if (0 === strpos(ACTION_NAME, C('VAR_PAGE'))) {
                $p = explode('-', ACTION_NAME);
                $_GET[C('VAR_PAGE')] = $p[1];
                $_REQUEST[C('VAR_PAGE')] = $p[1];
            }
            $ids = $this->categoryModel->getAllIDBypid($cate['id'], __RELATEID__);
            $map['n.category_id'] = array('in', $ids);
            $map['n.status'] = 1;
            $map['n.site_id'] = __RELATEID__;
            //SEO
            $seo['title'] = empty($cate['page_title']) ? $cate['title'] : $cate['page_title'];
            $seo['keywords'] = $cate['keywords'];
            $seo['description'] = $cate['description'];
            $this->assign('seo', $seo);
            $this->initView();

            $pagesize = $this->getPagesize();
            if ($pagesize <= 0) {
                $pagesize = 15;
            }
            $this->newsModel->_list($this->view, $map, 'n.sort,n.datetime desc', intval($pagesize));

            $list = $this->view->get('list');
            foreach ($list as $k => $v) {
                $list[$k]['extends'] = getNewsExtends($v['id']);
            }
            if (isset($_GET['dump'])) {
                dump($list);
            }



            $this->assign('list', $list);
            //读取模块

            $this->getCommomModules($this->view);
            $modulesList = $ModulesDB->getModulesList(__SITEID__, 'list-' . $this->template, $this->view->get());


            $this->assign('modulesList', $modulesList);
            $this->display('list:' . $this->template);
        } else {
            $id = intval(ACTION_NAME);
            $data = $this->newsModel->where(array('id' => $id, 'status' => 1, 'site_id' => __RELATEID__))->find();
            if (!array($data)) {
                $this->error('页面不存在');
            } else {
                if ($data['permission'] >= 0) {
                    if (!session('memberInfo')) {
                        $this->error("此内容必须登录才能查看", U('Member/Public/login'));
                    } else {
                        if (intval($this->memberInfo['socre']) < intval($data['permission'])) {
                            $this->error("您积分不够,无法查看此内容");
                        }
                    }
                }
            }


            $this->newsModel->where(array('id' => $id, 'status' => 1, 'site_id' => __RELATEID__))->data(array('click' => $data['click'] + 1))->save();
            //上一个
            $this->prev = $this->newsModel->where('id<' . $id . ' and status=1 and category_id=' . $cate['id'])->find();
            //下一个
            $this->next = $this->newsModel->where('id>' . $id . ' and status=1 and category_id=' . $cate['id'])->find();

            //SEO
            $seo['title'] = empty($data['page_title']) ? $data['title'] : $data['page_title'];
            $seo['keywords'] = $data['keywords'];
            $seo['description'] = $data['description'];
            $this->assign('seo', $seo);
            if (!empty($data['mimg_url'])) {
                $this->img_list = explode(',', $data['mimg_url']);
            }
            //$extend=M("News_extends")->where(array('news_id'=>$data['id']))->select();
            $this->assign('extend', getNewsExtends($data['id']));
            $this->assign($data);
            $this->assign('relate', unserialize($data['relate']));
            if ($data['outlink'] != '') {
                header("location:" . $data['outlink']);
            }
            //存在会员推荐

            if (isset($_COOKIE['affid']) && intval($_COOKIE['affid']) > 0) {
                $mdb = M('Member');
                $this->aff = $mdb->where(array('id' => $_COOKIE['affid']))->find();
            }
            //读取模块
            $this->initView();

            $modulesList = $ModulesDB->getModulesList(__SITEID__, 'view-' . $this->template, $this->view);

            $this->getCommomModules($this->view);
            $this->assign('modulesList', $modulesList);
            $this->display('view:' . $this->template);
        }
        exit();
    }

    //不存在这个页面时
    public function _empty()
    {
        //dump($_REQUEST);
        ob_end_clean();
        header('HTTP/1.1 404 Not Found');
        header("status: 404 Not Found");
        exit();
        //echo "该页面不存在";
    }

    //获取当前模板文件名
    public function getCurModelName()
    {
        $template = '';
        foreach ($this->pagenav as $k => $v) {
            if ($v['Tpl'] != '') {
                $template = $v['Tpl'];
            }
        }
        return $template;
    }

    public function getPagesize()
    {
        $pagesize = 0;
        foreach ($this->pagenav as $k => $v) {
            if ($v['pagesize'] > 0) {
                $pagesize = $v['pagesize'];
            }
        }
        return $pagesize;
    }

    //获取页面导航
    public function getPageNav($pid = 0)
    {
        $arr = $this->getPageNavArr($pid);

        return array_reverse($arr);
        return $arr;
    }

    //获取页面导航数组
    public function getPageNavArr($pid = 0, $arr = array())
    {
        $re = $this->categoryModel->where(array('id' => $pid, 'site_id' => __RELATEID__))->find();
        if (!is_array($re)) {
            return $arr;
        } else {
            $arr[] = $re;
            return $this->getPageNavArr($re['pid'], $arr);
        }
    }

    //获取所有子类ID包含自身
    public function getAllSubCate_id($cate, $isIncludeSelf = true)
    {
        $ids = $this->getAllSubCate($cate['id']);

        if ($isIncludeSelf) {
            $ids[] = $cate;
        }
        foreach ($ids as $k => $v) {
            $str[] = $v['id'];
        }
        return implode(',', $str);
    }

    //获取所有子类
    public function getAllSubCate($id = 0, $arr)
    {
        return $this->categoryModel->getListToArray1($id, __RELATEID__);
    }
}

?>